package com.witype.timeline.line;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by WiType on 2017/4/16.
 * Email witype716@gmail.com
 * Desc:
 */

public class DataUtils {

    public static long convertString2Milli(String time) {
        return convertString2Milli(time,"yyyy-MM-dd HH:mm:ss");
    }

    public static long convertString2Milli(String time,String format) {
        Date strToDate;
        try {
            DateFormat formatter = new SimpleDateFormat(format, Locale.CHINA);
            ParsePosition pos = new ParsePosition(0);
            strToDate = formatter.parse(time, pos);
        } catch (Exception e) {
            return 0;
        }
        return strToDate == null ? 0 : strToDate.getTime();
    }

    public static String convertMilli2String(long timestamp) {
        return convertMilli2String(timestamp,"yyyy-MM-dd HH:mm:ss");
    }

    public static String convertMilli2String(long timestamp,String format) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return formatter.format(timestamp);
    }
}
